<template>
	<view  >
		<view class="user-info">
			<button class="avatar" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" :disabled="show">
				<image :src="reversed_imageurl" class="avatar-img"></image>
			</button>
			<!-- <image class="u-img" @error="onHeaderError()" :src="UserInfo.avatar"></image> -->
			<view class="u-text">
				<view class="username">{{UserInfo.nickName}}</view>
				<view class="small-text">
					欢迎使用1111,{{getTimeText()}}
				</view>
			</view>
			 
		</view>
		<view class="home-content">
			 
			<view class="card-container">
				<!-- <view class="fn-title">基础功能</view> -->
				<u-grid :border="false"   col="4">
					<u-grid-item v-for="(item,index) in fn" :key="index">
						<view :class="['grid-item-bg','grid-item-bg-'+(index+1)]" @click="itemClick(item)">
							<image style="width:50rpx;height: 50rpx;" :src="item.icon"></image>
						</view>

						<view class="grid-text">{{item.name}}</view>
					</u-grid-item>
				</u-grid>
			</view>
		</view>
		<view class="home-content">
			 
			<view class="card-container" v-if="fn2.length!=0">
				<!-- <view class="fn-title">基础功能</view> -->
				<u-grid :border="false"  col="4">
					<u-grid-item v-for="(item,index) in fn2" :key="index">
						<view :class="['grid-item-bg','grid-item-bg-'+(index+1)]" @click="itemClick(item)">
							<image style="width:50rpx;height: 50rpx;" :src="item.icon"></image>
						</view>
		
						<view class="grid-text">{{item.name}}</view>
					</u-grid-item>
				</u-grid>
			</view>
		</view>
		
		<view class="u-menu-list" >
			<view class="u-menu-item" @click="itemClick(item)" v-for="(item,index) in menu" :key="index">
				<view class="u-menu-icon">
					<image style="width:36rpx;height: 36rpx;" :src="item.icon"></image>
					<!-- 	<u-icon :name="item.icon" color="#303133" size="15"></u-icon> -->
				</view>
				<view class="u-menu-text">{{item.name}}</view>
				<view class="u-menu-icon-rigth">
					<u-icon name="arrow-right" color="rgb(231 231 231)" size="15"></u-icon>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				height: 50,
				UserInfo: {},
				// userInfo: {
				// 	img: "/static/imgs/img01.png",
				// 	userName: "请登陆"
				// },
				show:true,
				avatarUrl: "",
				fn: [
					// {
					// 	name: "注册老师",
					// 	icon: '/static/flow.png',
					// 	path: "/pages/dz_teacher/dz_teacher_edit",
					// 	subPage: true //二级页面
					// },
					//  {
					//  		name: "发布家教",
					//  		icon: '/static/flow.png',
					//  		path: "/pages/dz_student/dz_student_edit",
					//  		subPage: true //二级页面
					//  	}, {
					//  		name: "我发布的家教",
					//  		icon: '/static/form.png',
					//  		path: "/pages/user/familyeducation/familyeducation_list",
					//  		subPage: true //二级页面
					//  	},



					 

				],
				fn2: [
					// {
					// 	name: "Table组件",
					// 	icon: '/static/fc.png',
					// 	path: "/pages/form/form",
					// 	subPage: true //二级页面
					// },
					// {
					// 	name: "菜单列表",
					// 	icon: '/static/table.png',
					// 	path: "/pages/menu/menu",
					// 	subPage: false //二级页面
					// } 
				
				
				
					 
				
				],

				menu: [{
						name: "教师/员工管1理",
						path: '/pages/dz_teacher/dz_teacher_list/dz_teacher_list',
						icon: '/static/user.png',
						color: ''
					},{
						name: "教室管理",
						path: '/pages/dz_classroom/dz_classroom_list/dz_classroom_list',
						icon: '/static/user.png',
						color: ''
					},{
						name: "商品/收费项管理",
						path: '/pages/dz_cost/dz_cost_list/dz_cost_list',
						icon: '/static/user.png',
						color: ''
					},{
						name: "账户与安全",
						path: '/pages/user/about/about',
						icon: '/static/user.png',
						color: ''
					}  ,
					 
					{
						name: "关于竹石Link",
						path: '/pages/user/about/about',
						icon: '/static/user.png',
						color: ''
					} 

					 
					// {
					// 	name: "退出登陆",
					// 	path: '/pages/login/login',
					// 	icon: '/static/logout.png',
					// 	color: ''
					// },
				]
			}
		},
		onLoad() {
			var _this = this;
			// 获取手机状态栏高度
			uni.getSystemInfo({
				success: function(data) {
					// 将其赋值给this
					_this.height = data.statusBarHeight;
				}
			})


		},
		onShow() {
			// this.getUserInfo();
			// console.log("走到这了")
			let UserInfo = this.$store.getters.getUserInfo();
			if (UserInfo != null) {

				this.UserInfo = UserInfo;
					this.show=false;
			}else{
				this.show=true;
			}

		},
		computed: {
			reversed_imageurl() {

				var str = this.http.ipAddress.replace(/\/$/, ''); // 将结尾的 "/" 替换为空字符串

				console.log(str);
				return str + this.UserInfo.avatar
			}
		},
		methods: {
			//更改头像
			onChooseAvatar(e) {
				console.log("www",UserInfo);
				let UserInfo = this.$store.getters.getUserInfo();
				
				if (UserInfo == null||UserInfo == "") {
				    this.$toast('请先进行登录')
					 
				}else{
					var that = this;
					const tempFilePath = e.detail.avatarUrl //上传的图片地址
					const maxSizeInBytes = 1024 * 1024 // 限制大小为1MB
					uni.getFileInfo({
						filePath: tempFilePath,
						success: res => {
							const fileSize = res.size
							if (fileSize > maxSizeInBytes) {
								//如果上传的图片大小超过1MB，进行提示
					
								this.$toast('请上传小于1MB的图片')
								return
							}
							//图片大小符合，替换图片
							this.avatarUrl = tempFilePath
							//将更改的图片上传到服务器
							uni.uploadFile({
								url: this.http.ipAddress + 'wechat/platformusers/updataimgsfile',
								filePath: this.avatarUrl,
								name: 'imgsfile',
								header: {
									// Authorization: 'Bearer ' + token // 将 token 添加到请求的 header 中
								},
								formData: {
									userId: this.UserInfo.userId,
					
					
								},
								success(res) {
									let fileRes = JSON.parse(res.data)
									console.log(fileRes);
									if (fileRes.code == 200) {
										that.$toast('上传成功')
										that.http.get("wechat/platformusers/" + that.UserInfo.userId, null,
											false).then(
											result => {
												if (result.code == 200) {
													that.UserInfo = result.data;
													that.$store.commit("setUserInfo", result.data);
												}
					
											})
					
					
									}
					
								}
							})
						}
					})
				}
				
			},


			// getUserInfo() {
			// 	this.http.post("api/user/getCurrentUserInfo", {}).then(x => {
			// 		//x.data.gender = x.data.gender;
			// 		//  this.$refs.form.reset(x.data);
			// 		this.userInfo.img = this.http.ipAddress + x.data.headImageUrl
			// 		this.userInfo.userName = x.data.userTrueName;
			// 		//this.userInfo.createDate = x.data.createDate;

			// 		//this.userInfo.phoneNo = x.data.phoneNo;
			// 		//this.userInfo.email = x.data.email;
			// 	});
			// },
			getTimeText() {
				let timeNow = new Date();
				// 获取当前小时
				let hours = timeNow.getHours();
				// 设置默认文字
				let text = ``;
				// 判断当前时间段
				if (hours >= 0 && hours <= 10) {
					text = `早上好`;
				} else if (hours > 10 && hours <= 14) {
					text = `中午好`;
				} else if (hours > 14 && hours <= 18) {
					text = `下午好`;
				} else if (hours > 18 && hours <= 24) {
					text = `晚上好`;
				}
				return text;
			},
			onHeaderError() {
				this.userInfo.img = "/static/imgs/img02.png";
			},
			test() {
				this.http.get("api/menu/getTreeMenu", {}, false).then(result => {
					console.log(result)
				})
			},
			toUserInfo() {
				uni.navigateTo({
					url: '/pages/user/modifyPwd/modifyPwd'
				})
			},
			itemClick(item) {
				console.log(item.path);
				// uni.navigateTo({
				// 			url: '/pages/login/login'
				
				// 		});
				if (!item.path) {
					return;
				}
				if (item.path == "/pages/login/login") {
					this.$store.commit("clearUserInfo");
					this.UserInfo = {};
					this.show=true;
					this.$toast("退出成功!");

					return;
				}
				uni.navigateTo({
					url: item.path
				})
				// if (item.path == "/pages/dz_teacher/dz_teacher_edit") {
				// 	let UserInfo = this.$store.getters.getUserInfo();
				// 	if (UserInfo != null) {
				// 		uni.navigateTo({
				// 			url: '/pages/dz_teacher/dz_teacher_edit'

				// 		});


				// 	} else {
				// 		uni.navigateTo({
				// 			url: '/pages/login/login'

				// 		});
				// 	}

				// 	return;
				// }


				// if (item.path == "/pages/dz_student/dz_student_edit") {
				// 	let UserInfo = this.$store.getters.getUserInfo();
				// 	if (UserInfo != null) {
				// 		uni.navigateTo({
				// 			url: '/pages/dz_student/dz_student_edit'

				// 		});

				// 	} else {
				// 		uni.navigateTo({
				// 			url: '/pages/login/login'

				// 		});
				// 	}
				// 	return;
				// }
				// if (item.path == "/pages/user/familyeducation/familyeducation_list") {
				// 	let UserInfo = this.$store.getters.getUserInfo();
				// 	if (UserInfo != null) {

				// 		uni.navigateTo({
				// 			url: '/pages/user/familyeducation/familyeducation_list?id=' + UserInfo
				// 				.userId // 要跳转到的页面路径
				// 		})

				// 	} else {
				// 		uni.navigateTo({
				// 			url: '/pages/login/login'

				// 		});
				// 	}
				// 	return;
				// }




				

			}
		}
	}
</script>

<style lang="less" scoped>
	.user-info {
		// height: 220rpx;
		display: flex;
		align-items: center;
		padding: 150rpx 40rpx 60rpx 60rpx;
		background-image: linear-gradient(135deg, #26bcff 10%, #078ef9);
		position: relative;

		.u-img {
			width: 150rpx;
			height: 150rpx;
			border-radius: 50%;
			border: 2rpx solid #FFFFFF;
		}

		.u-text {
			flex: 1;
			color: #FFFFFF;
			padding: 26rpx 30rpx;
		}

		.username {
			font-weight: bolder;
			font-family: 黑体;
		}

		.small-text {
			font-size: 24rpx;
			padding-top: 10rpx;
		}

		.u-icon-setting {
			width: 30rpx;
			color: #FFFFFF;
		}
	}

	.u-menu-list {
		background: #FFFFFF;
		margin: 20rpx;
		border-radius: 5rpx;
		border: 1px solid #f7f7f7;

		.u-menu-item {
			display: flex;
			padding: 30rpx 20rpx;
			border-bottom: 1px solid #f7f7f7;
			align-items: center;

			.u-menu-icon {
				padding-top: 8rpx;
				padding-right: 20rpx;
			}

			.u-menu-text {
				flex: 1;
				color: #5e5e5e;
			}

			.u-menu-icon-rigth {
				width: 30rpx;
			}
		}
	}

	.avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50rpx;
		border: 2rpx solid white;
		box-shadow: 0 1rpx 5rpx black;
		padding: 0;

		.avatar-img {
			width: 100%;
			height: 100%;
		}
	}
	
.home-content {
		// z-index: 999;
		// position: relative;
		// margin-top: -220rpx;
	}

	.app-name {
		text-align: center;
		color: #ffff;
		font-weight: bolder;
		font-size: 60rpx;
		top: -40rpx;
		position: relative;
	}

	.card-container {

		box-shadow: 1px 1px 9px #b9b6b629;
		margin: 30rpx 30rpx 30rpx 30rpx;
		border: 1px solid #f1f1f1;
		border-radius: 10rpx;
		padding: 26rpx 10rpx 14rpx 10rpx;
		background: #ffff;


		.fn-title {
			font-family: 黑体;
			font-size: 30rpx;
			font-weight: bold;
			//color: #8f9ca2;
			padding: 4rpx 20rpx 30rpx 20rpx;
		}

		.grid-text {
			padding-top: 8rpx;
			font-size: 26rpx;
			color: #626262;
			padding-bottom: 20rpx;
		}

	}

	.grid-item-bg {
		border-radius: 50%;
		width: 86rpx;
		height: 86rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 5px 3px 6px #e0ddddb0;
	}

	.grid-item-bg-1 {
		background-image: linear-gradient(to bottom right, #97caff, #47a1fe);
	}

	.grid-item-bg-2 {
		background-image: linear-gradient(to bottom right, #f8bcbc, #f07e7e);

	}

	.grid-item-bg-3 {
		background-image: linear-gradient(to bottom right, #afb5e6, #808cf0);
	}

	.grid-item-bg-4 {

		background-image: linear-gradient(to bottom right, #98e4e2, #56c3bf);
	}

	.grid-item-bg-5 {
		background-image: linear-gradient(to bottom right, #d1d1d1, #c878e7);
	}

	.grid-item-bg-6 {
		background-image: linear-gradient(to bottom right, #97caff, #47a1fe);
	}

	.grid-item-bg-7 {
		background-image: linear-gradient(to bottom right, #98e4e2, #56c3bf);

	}

	.grid-item-bg-8 {
		background-image: linear-gradient(to bottom right, #afb5e6, #808cf0);

	}
</style>
